package com.njzxw.fs.commonutil.bean;

import com.njzxw.fs.commonutil.repository.BaseRep;
import com.njzxw.fs.commonutil.util.RedisUtil;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 常用帮助类
 */
@Getter
@Setter
@ToString
@Service
public class Common {

    public org.slf4j.Logger logger = LoggerFactory.getLogger(this.getClass());

    /**
     * 默认缓存名称
     */
    private String CacheName = "defaultCache";

    /**
     * 用户信息缓存名称
     */
    private String userCacheName = "userCache";

    /**
     * redis中店铺配置表标识前缀
     */
    private String deployCacheName = "deploy_";

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private BaseRep baseRep;

    /**
     * 后台所有请求的url链接
     */
    private List<String> urlList = new ArrayList<String>();

    private List<SystemGroupBean> systemGroupList;

    /**
     * 获取系统设置单挑值 针对店铺 区分
     *
     * @param groupCode
     * @return
     */
    public String getSystem(String groupCode,String shop_id) {

        String key = getDeployCacheName().concat("0").concat(shop_id);

        List<SystemGroupBean> systemGroupList = redisUtil.getValueObject(key,List.class);
        logger.debug("systemGroupList:"+systemGroupList);
        if(systemGroupList == null || systemGroupList.size() == 0){//没有值就重新传一次
            setDeploy(shop_id,"0");
            systemGroupList = redisUtil.getValueObject(key,List.class);
        }

        try{
            for (int i = 0; i < systemGroupList.size(); i++) {
                if (systemGroupList.get(i).getGroupcode().equals(groupCode)) {
                    return systemGroupList.get(i).getValues();
                }
            }
        }catch (Exception e){
            logger.error("获取配置表数据错误：",e);
        }

        return "";
    }

    /**
     * 清除配置表缓存信息
     *
     * @param shop_id
     * @param type  0 多项 1单项
     */
    public void clearSystem(String shop_id,String type){
        try{
            String key = getDeployCacheName().concat(type).concat(shop_id);
            redisUtil.deleteKeyObject(key);
        }catch (Exception e){
            logger.error("清除配置表单项缓存失败",e);
        }
    }

    /**
     * 获取系统设置单挑值 针对店铺 区分
     *
     * @param groupCode
     * @return
     */
    public List<SystemItemBean> getSystemList(String groupCode, String shop_id) {

        String key = getDeployCacheName().concat("1").concat(shop_id);

        List<SystemGroupBean> systemGroupList = redisUtil.getValueObject(key,List.class);

        if(systemGroupList == null || systemGroupList.size() == 0){//没有值就重新传一次
            setDeploy(shop_id,"1");
            systemGroupList = redisUtil.getValueObject(key,List.class);
        }

        try{
            for (int i = 0; i < systemGroupList.size(); i++) {
                if (systemGroupList.get(i).getGroupcode().equals(groupCode)) {
                    return systemGroupList.get(i).getItemList();
                }
            }
        }catch (Exception e){
            logger.error("获取配置表数据错误：",e);
        }

        return new ArrayList<>();

    }

    /**
     * 根据店铺ID设置对应的配置表信息
     *
     * @param shop_id
     */
    private void setDeploy(String shop_id,String type){
        redisUtil.deleteKeyObject(getDeployCacheName().concat(shop_id));

        List<SystemGroupBean> groupList = baseRep.getSystemGroupSiglBeanShop(shop_id);
        groupList.addAll(baseRep.getSystemGroupMoreBeanShop(shop_id));

        for (int i = 0; i < groupList.size(); i++) {
            String groupcode = groupList.get(i).getGroupcode();
            String values = groupList.get(i).getValues();
            if (StringUtils.isBlank(values)) {
                groupList.get(i).setItemList(baseRep.getSystemGroupItemBeanShop(groupcode,shop_id));
            }
        }
        redisUtil.setKeyObject(getDeployCacheName().concat(type).concat(shop_id),groupList,30l, TimeUnit.DAYS);
    }

    /**
     * 获取系统设置单挑值,平台对应的配置表信息
     *
     * @param groupCode
     * @return
     */
    public String getSystem(String groupCode) {

       return getSystem(groupCode,"0");
    }

    /**
     * 平台对应的配置表信息
     *
     * @param groupCode
     * @return
     */
    public List<SystemItemBean> getSystemList(String groupCode) {

        return getSystemList(groupCode,"0");
    }

}
